﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.Events
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.Events.Index.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.Events.Index.PageLoad();
            

            return data;
        }

        public static Adams.Admin.DataTransfer.Events.Index.EventCollection GetEvents(Adams.Admin.DataTransfer.Events.Index.GetEventsInput input)
        {
            var data = new Adams.Admin.DataTransfer.Events.Index.EventCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT O.* , C.Color
FROM Events.Occurances O
INNER JOIN Events.Events E ON O.EventID = E.EventID
INNER JOIN Events.Calendars C ON C.CalendarID = E.CalendarID
WHERE DATEDIFF(d, @StartDate, O.StartDate) >= 0 AND DATEDIFF(d, O.StartDate, @EndDate) >= 0
", CommandType.Text,
    DB.CreateParameter("@StartDate", input.start),
    DB.CreateParameter("@EndDate", input.end)))
            {
                while (dr.Read())
                {
                    var e = new Adams.Admin.DataTransfer.Events.Index.Event();
                    e.id = dr["OccuranceID"].DbInt();
                    e.eventid = dr["EventID"].DbInt();
                    e.title = dr["Title"].DbString();
                    e.start = dr["StartDate"].DbDate().ToString();
                    e.end = dr["EndDate"].DbDate().ToString();
                    e.allDay = dr["IsAllDay"].DbBool();
                    e.color = dr["Color"].DbString();
                    data.Add(e);
                }
            }

            return data;
        }

        public static void MoveEvent(Adams.Admin.DataTransfer.Events.Index.EventMoveInput input)
        {
            DB.Run(@"
UPDATE Events.Occurances SET 
StartDate = DATEADD(d, @dayDelta, DATEADD(mi, @minuteDelta, StartDate)),
EndDate = DATEADD(d, @dayDelta, DATEADD(mi, @minuteDelta, EndDate))
--,IsAllDay = @allDay
WHERE OccuranceID = @OccuranceID
", CommandType.Text,
            DB.CreateParameter("@OccuranceID", input.occuranceid),
            DB.CreateParameter("@dayDelta", input.dayDelta),
            DB.CreateParameter("@minuteDelta", input.minuteDelta),
            DB.CreateParameter("@allDay", input.dayDelta));
        }

        public static void ResizeEvent(Adams.Admin.DataTransfer.Events.Index.EventResizeInput input)
        {
            DB.Run(@"
UPDATE Events.Occurances SET 
EndDate = DATEADD(d, @dayDelta, DATEADD(mi, @minuteDelta, EndDate))
WHERE OccuranceID = @OccuranceID
", CommandType.Text,
            DB.CreateParameter("@OccuranceID", input.occuranceid),
            DB.CreateParameter("@dayDelta", input.dayDelta),
            DB.CreateParameter("@minuteDelta", input.minuteDelta));
        }
    }
}
